<?php
/**
 * Sharing Model for Sharing World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/
 * @license		GNU/GPL
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

/**
 * Sharing Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class SharingModelSharing extends JModel
{
	var $db;
	function __construct()
	{
		parent::__construct();
		$this->db =& JFactory::getDBO();

		$id = JRequest::getVar('id', '0');
		$this->setId($id);
	}

	/**
	 * Method to set the sharing identifier
	 *
	 * @access	public
	 * @param	int Sharing identifier
	 * @return	void
	 */
	function setId($id)
	{
		// Set id and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}

	/**
	 * Gets the greeting
	 * @return string The greeting to be displayed to the user
	 */
	function getSharings()
	{

		if (!$eYear = JRequest::getVar( 'year' )) $eYear = date("Y");
		if (!$eMonth = JRequest::getVar( 'month' )) $eMonth = date("m");
		if (!$eDay = JRequest::getVar( 'day' )) $eDay = date("d");
		
		$query = 'SELECT greeting FROM #__sharing WHERE ';
		$this->db->setQuery( $query );
		$greeting = $this->db->loadResult();

		return $greeting;
	}
	
	
	function getSharingByDate($date)
	{
		//if date_end is set then use this
		//$query = "SELECT * FROM #__sharing WHERE e_end >= '" . $date . "' AND e_date <= '" . ($date + 86400) . "' ORDER BY e_date";
		
		$query = "SELECT * FROM #__sharing WHERE date >= '" . $date . "' AND date <= '" . ($date + 86390) . "' ORDER BY date";
		$this->db->setQuery( $query );
		$rows = $this->db->loadObjectList();
		return $rows;
	}
	/**
	 * Returns the query
	 * @return string The query to be used to retrieve the rows from the database
	 */
	function _buildQuery()
	{
		$query = ' SELECT * '
			. ' FROM #__sharing '
		;

		return $query;
	}

	/**
	 * Retrieves the sharing data
	 * @return array Array of objects containing the data from the database
	 */
	function getData()
	{
		// Lets load the data if it doesn't already exist
		if (empty( $this->_data ))
		{
			$query = $this->_buildQuery();
			$this->_data = $this->_getList( $query );
		}

		return $this->_data;
	}

	function store($data)
	{
		$row =& $this->getTable();

		// Bind the form fields to the table
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Make sure the web link table is valid
		if (!$row->check()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Store the data table to the database
		if (!$row->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		return true;
	}

	
}
